<?php

class wxapi_ctl_address extends wxapi_response
{
    function __construct(&$app){
         parent::__construct($app);
         $this->member_id = 11;
         $this->addr_id = 6;
    }


    /**
     * @param int $member_id 用户ID
     * @return array $addrInfo 获取用户
     */
    public function getMemberAddrsList()
    {
        $member = $this->member_info;
        if( !($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        $objMem = app::get('b2c')->model('members');
        $addrInfo = $objMem->getMemberAddr($member['member_id']);
        return self::api_success($addrInfo);
    }

    /**
     * @param array $addrInfo 保存用户信息
     * @return boolean $status 保存或更新信息
     */

    public function saveOrUpdateAddr($addrInfo)
    {

        $member = $this->member_info;
        if( !($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        $precessInfo = [];
        $addr_id = $addrInfo['addr_id'] ? $addrInfo['addr_id'] : null;
        $addrObj = app::get('b2c')->model('member_addrs');

        if ($addr_id)
        {
            $flag = 'update';
        } else {
            $flag = 'save';
        }

        $precessInfo['member_id'] = $member ? $member['member_id'] : "";
        $precessInfo['name']      = $addrInfo['name'] ? $addrInfo['name'] : "";
        $precessInfo['area']      = $addrInfo['area'] ? $addrInfo['area'] : "";
        $precessInfo['zip']       = $addrInfo['zip'] ? $addrInfo['zip'] : "";
        $precessInfo['mobile']    = $addrInfo['mobile'] ? $addrInfo['mobile'] : "";
        $precessInfo['tel']       = $addrInfo['tel'] ? $addrInfo['tel'] : "";

        if ($flag == 'save')
        {
            $status = $addrObj->insert($precessInfo);
        } else {
            $status = $addrObj->update($precessInfo,['addr_id' => $addr_id]);
        }

        if ($status == true)
        {
        	return self::api_success();
        } else {
        	return self::api_error();
        }
    }


    /**
     * @param int $addr_id 会员地址id
     * @return array $addInfo 地址信息
     */
    public function getInfoById($addr_id)
    {
        $addrObj = app::get('b2c')->model('member_addrs');
        $addrInfo = $addrObj->getRow('*',['addr_id' => $this->addr_id]);

        return $addrInfo ? self::return_json($addrInfo) : [];
    }


    /**
     * @param int $addr_id 会员地址信息
     * @param Boolean $status 删除状态
     */
    public function delAddrById($addr_id)
    {

        $addrObj = app::get('b2c')->model('member_addrs');
        $status  = $addrObj->delete(['addr_id' => $this->addr_id]);

        if ($status == true)
        {
        	return self::api_success();
        } else {
        	return self::api_error();
        }
    }
}